package com.dragon.client.controller.avttendance;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.logical.service.avttendance.IAnnouncementService;
import com.dragon.repository.domain.dto.AnnouncementDTO;
import com.dragon.repository.domain.vo.AnnouncementVO;
import com.dragon.repository.entity.Announcement;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author sy
 * @since 2023-07-26
 */
@RestController
@RequestMapping("announcement")
@Api(tags = "公告")
public class AnnouncementController {

    @Autowired
    IAnnouncementService service;

    @GetMapping("list")
    @ApiOperation("公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码",required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, type = "int")
    })
    public ResponseData<List<AnnouncementDTO>> announcementList(Integer page, Integer limit){
        Page<Announcement> list = service.announcementList(page, limit);
        return ResponseData.success(AnnouncementDTO.convertList(list.getRecords()));
    }

    @PostMapping("add")
    @ApiOperation("添加公告")
    public ResponseData add(@RequestBody @Validated(Insert.class) AnnouncementVO vo){
        service.add(AnnouncementVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delelte")
    @ApiOperation("删除公告")
    public ResponseData delete(@ApiParam(name = "id", value = "id", required = true, type = "int")
                               @RequestParam Integer id){
        service.delete(id);
        return ResponseData.success();
    }

    @PostMapping("disable")
    @ApiOperation("启用禁用公告")
    public ResponseData DisableAnnounce(@ApiParam(name = "id", value = "id", required = true, type = "int")
                               @RequestParam Integer id){
        service.DisableAnnounce(id);
        return ResponseData.success();
    }

    @PostMapping("getLatest")
    @ApiOperation("获取最新公告")
    public ResponseData<AnnouncementDTO> getLatestOne(){
        Announcement latestOne = service.getLatestOne();
        return ResponseData.success(AnnouncementDTO.convert(latestOne));
    }

}
